<?php
include("dao/DatabaseDAO.php");
include("dao/XMLFeedDAO.php");
include("crontab/Crontab.php");
include("Security.php");

Security::checkAuthenticated();

$data = $_POST["data"];
$secondaryTablesMappings = $_POST["secondaryTablesMappings"];

$databaseId = $_POST["databaseId"];
$feedName = $_POST["feedName"];
$feedUrl = $_POST["feedUrl"];
$xmlRootItem = $_POST["xmlRootItem"];
$feedTable = $_POST["feedTable"];
$keyColumn = $_POST["keyColumn"];
$repeatTime = $_POST["repeatTime"];
$userId = $_SESSION["UserId"];

$xmlFeed = Array(
    "UserId" => $_SESSION["UserId"],
    "Name" => $feedName,
    "URL" => $feedUrl,
    "RepeatTime" => $repeatTime,
    "DatabaseId" => $databaseId,
    "XmlRootItem" => $xmlRootItem,
);

$databaseDAO = new DatabaseDAO();
$database = $databaseDAO->getDatabase($databaseId);

$xmlFeedDAO = new XMLFeedDAO();

$xmlFeedId = $xmlFeedDAO->saveXMLFeed($xmlFeed);

$xmlFeedDAO->saveKeyColumn($keyColumn, $xmlFeedId);

$xmlTableMapping = Array(
    "XmlFeedId" => (int)$xmlFeedId,
    "Table" => $feedTable,
    "Type" => "Primary"
);
$xmlTableMappingId = $xmlFeedDAO->saveXMLTableMapping($xmlTableMapping);

foreach ($data as $key => $values) {
    foreach ($values as $column => $value) {
        $xmlColumnMapping = Array(
            "XmlFeedTableId" => $xmlTableMappingId,
            "XmlField" => $value["value"],
            "XmlValueType" => $value["type"],
            "DatabaseColumn" => $column,
            "ActionId" => (empty($value["action"]) ? NULL : $value["action"]),
            "Default" => $value["default"],
            "DependsOn" => $value["dependsOn"],
            "IsKey" => $value["key"],
            "TagIndex" => $value["index"],
        );

        $xmlFeedDAO->saveXMLColumnMapping($xmlColumnMapping);
    }
}

for ($i = 0; $i < count($secondaryTablesMappings); $i++) {
    $table = $secondaryTablesMappings[$i]["table"];
    $xmlTableMapping = Array(
        "XmlFeedId" => (int)$xmlFeedId,
        "Table" => $table,
        "Type" => "Secondary"
    );
    $xmlTableMappingId = $xmlFeedDAO->saveXMLTableMapping($xmlTableMapping);

    $mapping = $secondaryTablesMappings[$i]["mapping"];
    foreach ($mapping as $key => $values) {
        foreach ($values as $column => $value) {
            $xmlColumnMapping = Array(
                "XmlFeedTableId" => $xmlTableMappingId,
                "XmlField" => $value["value"],
                "XmlValueType" => $value["type"],
                "DatabaseColumn" => $column,
                "ActionId" => (empty($value["action"]) ? NULL : $value["action"]),
                "Default" => $value["default"],
                "DependsOn" => $value["dependsOn"],
                "IsKey" => $value["key"],
                "TagIndex" => $value["index"],
            );

            $xmlFeedDAO->saveXMLColumnMapping($xmlColumnMapping);
        }
    }
}

if (isset($repeatTime) && $repeatTime != 0) {
    $config = $xmlFeedDAO->getConfig("TaskRunnerPath");
    $cronExpression = "0 0 12 */$repeatTime *";
//    Crontab::addXmlJob($cronExpression, $config, $userId, $xmlFeedId);

    $xmlFeedUpdate = Array(
        "CronExpression" => $cronExpression
    );

    $xmlFeedDAO->updateXmlFeed($xmlFeedUpdate, $xmlFeedId);
}

$providedXmlFeedId = $_POST["xmlFeedId"];
if (isset($providedXmlFeedId)) {
    $xmlFeedDAO->deleteXmlFeed($providedXmlFeedId, $userId);
}

//Add cron job
//$output = shell_exec('crontab -l');
//file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
//exec('crontab /tmp/crontab.txt');

header("location:../manage.php");
?>